<?php
/**
 * Created by PhpStorm.
 * User: xeng
 * Date: 2018/1/25
 * Time: 11:44
 */

/*
 * 推广员 类
 */
namespace Home\Controller;
use Think\Controller;

class SpreaderController extends AuthController {
    /*
     * 默认推广员 列表 查询
     */
    public function index(){
        //分页查询
        $p=$_GET['p']?$_GET['p']:1;         //第几页
        $listRows=50 ;                      //每页记录数
        $firstRow =($p-1)*$listRows;        // dump($firstRow);

        $lists = $this->sqlsrv1("select top $listRows accountid, account,mypromocode,invitecode,regtime FROM UserAccount WHERE accountid not in(SELECT top $firstRow accountid FROM UserAccount); ");
        $count = $this->sqlsrv1("select count(*) FROM UserAccount ");
        $show = $this->page_show($count,10);
        $i=0; //指针
        foreach ($lists as $vo){
            $result[$i]['accountid'] = $vo['accountid'];
            $result[$i]['account'] = $vo['account'];
            $result[$i]['mypromocode'] = $vo['mypromocode'];
            $result[$i]['invitecode'] = $vo['invitecode'];
            $result[$i]['regtime'] = $vo['regtime']->date;
            $i++;
        }

        $this->assign('lists',$result);
        $this->assign("page_show",$show);
        $this->display();
    }
    /*
     * 条件搜索结果
     * $_POST
     */
    public function search(){
        //拿到条件 分页查询  dump($_POST);
        $con =$_POST;//保留查询条件

        $p=$_GET['p']?$_GET['p']:1;         //第几页
        $listRows=50 ;                      //每页记录数
        $firstRow =($p-1)*$listRows;

        //如果查accountid
        $accountid = $_POST['gameid'];
        $id_str = strval(trim($accountid));
        $id_int = intval($accountid);        // echo strlen($id_int)."----"; echo strlen($id_str);
        if(strlen($id_str)==strlen($id_int) && strlen($id_str)!=0){
            $lists = $this->sqlsrv1("SELECT accountid, account,mypromocode,invitecode,regtime FROM UserAccount WHERE gameid=$id_int;");
        }elseif($_POST['start_time']!=""  or  $_POST['end_time']!=""  ){
            //时间段查询
            $start_time=$_POST['start_time']?$_POST['start_time']:"1900-01-01";
            $end_time = $_POST['end_time']?$_POST['end_time']:date('Y-m-d ',time());
            $start_time = "'".$start_time."'";
            $end_time ="'".$end_time."'"; //dump($start_time) ; echo $end_time;

            $lists = $this->sqlsrv1("SELECT TOP 10 accountid,	account,mypromocode,invitecode,	regtime
                FROM	UserAccount
                WHERE	gametid NOT IN (	SELECT	TOP 1 accountid	FROM	UserAccount	)
                AND regtime BETWEEN  $start_time  AND  $end_time
            ");
            $count = $this->sqlsrv1("select count(*) FROM UserAccount  WHERE  regtime BETWEEN  $start_time  AND  $end_time");
            $show = $this->page_show($count,10);   // dump($lists[0]["regtime"]->date);
        }

        $this->assign('lists',$lists);
        $this->assign('con',$con);
        $this->assign("count",reset(reset($count)));
        $this->assign('show',$show);
        $this->display('search');

    }

    /*
     * 每个会员的推广详情
     */
    public function detail(){
        //dump($_GET);
        $accountid=$_GET['id'];
        if(!$accountid){
            echo "没有查询ID";
            die;
        }
        //get mypromocode by accountid
        if($_GET['id']){
            $my = $this->sqlsrv1("SELECT accountid,mypromocode,invitecode FROM UserAccount WHERE accountid=$accountid");
        }
        $data = reset($my);

        if($data['invitecode']){
            $invitecode=$data['invitecode'];
        }else{
            $invitecode="没有上级";
        }
        if($data['mypromocode']){
            $mypro1 ="'".$data['mypromocode']."'";//dump($mypro1);
            //1 级推广
            $mypromocode1 = $this->sqlsrv1("SELECT accountid,mypromocode,invitecode FROM UserAccount WHERE invitecode=$mypro1");

            //2 级推广码
            foreach ($mypromocode1 as $key=>$volue){       //dump($volue['mypromocode']);
                if($volue['mypromocode']){
                    $mypro2 = "'".$volue['mypromocode']."'";
                    $mypromocode2[] = $this->sqlsrv1("SELECT accountid,mypromocode,invitecode FROM UserAccount WHERE invitecode=$mypro2" );
                }
            }
        }
        if(reset($mypromocode2)!=''){
            foreach ($mypromocode2 as $key=>$value){
                foreach ($value as $k=>$v){
                    $mypro3 = "'".$v['mypromocode']."'";
                    $mypromocode3[] = $this->sqlsrv1("SELECT accountid,mypromocode,invitecode FROM UserAccount WHERE invitecode= $mypro3 ");
                }
            }
        }

        $this->assign("my",$my);        //当下查询对象
        $this->assign('mypromocode1',$mypromocode1);    //下级 1
        $this->assign('mypromocode2',$mypromocode2); // 下级 2
        $this->assign('mypromocode3',$mypromocode3); //下级 3
        $this->assign('invitecode',$invitecode);  //上级
        $this->display('detail');
    }

}

